using Light;
using UnityEngine;

namespace Dream.ThirdLevel
{
    public class PhysicsCheck : MonoBehaviour
    {
        public AudioClip bgm;

        [Header("检测参数")] public bool maual;

        public Vector2 bottomOffset;
        public Vector2 leftOffset;
        public Vector2 rightOffset;
        public float checkRaduis;
        public LayerMask groundLayer;

        [Header("状态")] public bool isGround;

        public bool touchLeftWall;
        public bool touchRightWall;

        private BoxCollider2D coll;

        private void Awake()
        {
            isGround = true;
            coll = GetComponent<BoxCollider2D>();

            if (!maual)
            {
                rightOffset = new Vector2((coll.bounds.size.x + coll.offset.x) / 2 + 0.2f, coll.size.y / 2);
                leftOffset = new Vector2(-rightOffset.x, rightOffset.y);
            }
        }

        private void Start()
        {
            AudioManager.Instance.setBGM(bgm);
        }

        private void Update()
        {
            Check();
        }

        private void OnDrawGizmosSelected()
        {
            Gizmos.DrawWireSphere((Vector2)transform.position + bottomOffset, checkRaduis);
            Gizmos.DrawWireSphere((Vector2)transform.position + leftOffset, checkRaduis);
            Gizmos.DrawWireSphere((Vector2)transform.position + rightOffset, checkRaduis);
        }

        public void Check()
        {
            // 地面检测
            isGround = Physics2D.OverlapCircle((Vector2)transform.position + bottomOffset, checkRaduis, groundLayer);

            // 墙面检测
            touchLeftWall = Physics2D.OverlapCircle((Vector2)transform.position + leftOffset, checkRaduis, groundLayer);
            touchRightWall =
                Physics2D.OverlapCircle((Vector2)transform.position + rightOffset, checkRaduis, groundLayer);
        }
    }
}